
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>简易论坛</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background: #f0f2f5;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .navbar {
            background: #fff;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .post-list {
            background: white;
            border-radius: 8px;
            padding: 20px;
        }
        .post-card {
            padding: 15px;
            border-bottom: 1px solid #eee;
            transition: 0.3s;
        }
        .post-card:hover {
            background: #f8f9fa;
        }
        .post-title {
            font-size: 18px;
            color: #1a1a1a;
            margin-bottom: 8px;
        }
        .post-meta {
            color: #666;
            font-size: 14px;
        }
        .new-post-btn {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            text-decoration: none;
            margin-bottom: 20px;
            display: inline-block;
        }
        .sidebar {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-left: 20px;
            width: 300px;
        }
        .main-content {
            display: flex;
        }
        .pagination {
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar">
    <h1>简易论坛</h1>
    <div>
        <?php
        session_start();

        if($_SESSION['username'] == null){
            ?>
        <a href="login.php">登录</a> | <a href="register.php">注册</a>
        <?php }else{ ?>
            <a href="user_edit.php"><img src="<?php echo $_SESSION['img'] ?>" alt="头像"></a>
            <br>
            <a href="logout.php">退出登录</a>
        <?php } ?>
    </div>
</nav>
<div class="main-content">
    <!-- 主内容区 -->
    <div style="flex: 1">
        <!-- 发帖按钮 -->
        <a href="savewenzhang.php" class="new-post-btn">+ 发布新帖</a>

        <!-- 帖子列表 -->
        <div class="post-list">
            <?php
            include 'conn.php';
            $search ='%'. $_GET['search'] .'%';
            $user_id = $_SESSION['id'];
            if($_SESSION['username'] == null){
                $sql = "select id,title, user_id,time,level from article where (level = 0) and (body  like ? or title like ? );";
            }
            elseif($_SESSION['level']){
                $sql = "select id,title, user_id,time,level from article where body  like ? or title like ? ;";
            }
            else {
                $sql = "select id, title, user_id,time,level from article where (level = 0 or level = 1 or (level = 2 and user_id = $user_id)) and (body  like ? or title like ?);";
            }
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("ss", $search, $search);
            $stmt->execute();
            $article_info= $stmt->get_result();
            if ($article_info->num_rows > 0) {
                // 输出数据
                while($row = $article_info->fetch_assoc()) {
                ?>
                    <div class="post-card">
                        <a href="main.php?id=<?php echo $row['id'] ?>">
                            <h2 class="post-title"><?php echo $row['title'] ?></h2>
                        </a>
                        <?php
                        $sql = "select username,name from users where id = ".$row['user_id'].';';
                        $name_info = $conn->query($sql);
                        $autor_array = $name_info->fetch_assoc();


                        ?>
                        <span>作者：<?php if(!$autor_array['name']){echo $autor_array['username'];}else{echo $autor_array['name'];} ?></span>
                        <br>
                        •<span><?php echo $row['time']; ?></span> •
                        <?php if ($row['user_id'] == $_SESSION['id'] or $_SESSION['level']){
                            ?>
                            <a href="article_edit.php?id=<?php echo $row['id'] ?>">修改</a> | <a href="delete_article.php?id=<?php echo $row['id'] ?>">删除</a>
                            <?php
                        } ?>
                    </div>
            <?php
                }
            }
            $conn->close();
            ?>
            <!-- 单个帖子 -->



        </div>
        <!-- 分页 -->
        <div class="pagination">
            <a href="#">上一页</a> | <a href="#">1</a> | <a href="#">2</a> | <a href="#">下一页</a>
        </div>
    </div>
    <!-- 侧边栏 -->
    <div class="sidebar">
        <h3>搜索</h3>
        <ul>
            <form action="" method="get">
                <input type="text" name="search"><button type="submit">搜索</button>
            </form>
        </ul>
    </div>
</div>
</body>
</html>
